package com.dementors.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.dementors.R;
import com.dementors.base.BaseActivity;
import com.dementors.base.SpaceItemDecoration;
import com.dementors.data.API;
import com.dementors.model.Goods;
import com.dementors.net.http.okhttp.callback.OkHttpCallback;
import com.dementors.net.http.okhttp.request.OkHttpRequest;
import com.dementors.ui.adapter.ItemAdapter;
import com.dementors.utils.TimeUtils;
import com.dementors.view.RecyclerViewHeader;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;

/**
 * ***********************
 * author:raochen
 * time:2015/12/8.
 * name:点击商品后显示的页面
 * step:
 * ***********************
 */
public class ItemActivity extends BaseActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private final int BIGIMAGE=0;
    private final int ICON=2;
    private final int UPTIME=1;
    private com.dementors.view.RecyclerViewHeader recyclerviewheader;
    private RadioGroup rg_selector;
    private RecyclerView recyclerview_item_activity;
    private RecyclerViewHeader header;
    private ArrayList<Goods.Show> list;
    private ItemAdapter mAdapter;
    private int id;//点击item的id
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case BIGIMAGE:
                    recyclerviewheader.setBigImg((Bitmap) msg.obj);
                    break;
                case ICON:
                    recyclerviewheader.setIconImg((Bitmap) msg.obj);
                    break;
                case UPTIME:
                    int time=(Integer) msg.obj;
                    recyclerviewheader.setTime(TimeUtils.getTimeLeft(time));
                    Message message=Message.obtain();
                    message.obj=time-1;
                    message.what=UPTIME;
                    handler.sendMessageDelayed(message,1000);

                    break;
            }

        }
    };
    @Override
    protected int provideContentViewId() {

        return R.layout.activity_item;
    }

    @Override
    protected void initView() {
        rg_selector= (RadioGroup) findViewById(R.id.rg_selector);
        recyclerview_item_activity= (RecyclerView) findViewById(R.id.recyclerview_item_activity);
        recyclerviewheader= (RecyclerViewHeader) findViewById(R.id.recyclerviewheader);
        toolbar= (Toolbar) findViewById(R.id.toolbar);

    }

    @Override
    protected void setListener() {
        mAdapter.setMyItemClickListener(new ItemAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(ItemActivity.this, "加入购物车", Toast.LENGTH_SHORT).show();
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        header=new RecyclerViewHeader(this);
        recyclerview_item_activity.setLayoutManager(new GridLayoutManager(this,2));
        list=new ArrayList<>();
        mAdapter=new ItemAdapter(this);
        mAdapter.setList(list);
        recyclerview_item_activity.addItemDecoration(new SpaceItemDecoration(10));
        recyclerview_item_activity.setAdapter(mAdapter);
        id=getIntent().getIntExtra("id",0);

        //获取网络资源
        getHttp();
        initToolBar();
    }

    private void initToolBar() {
        // App Logo
        toolbar.setNavigationIcon(R.mipmap.qr_back);
        //APP backgroudcolor
        toolbar.setBackgroundColor(getResources().getColor(R.color.md_red_100));
        setSupportActionBar(toolbar);
    }

    private void getHttp() {

        new OkHttpRequest.Builder().url(API.ITEM_URL_1+id+API.ITEM_URL_2).get(new OkHttpCallback<Goods>() {
            @Override
            public void onError(Request request, Exception e) {

            }
            @Override
            public void onResponse(Response response, final Goods data) {
                toolbar.setTitle(data.seller_title);
                if(data.martshow_items!=null) {
                    list.addAll(data.martshow_items);
                    mAdapter.setList(list);
                    mAdapter.notifyDataSetChanged();
                    Message message = Message.obtain();
                    message.obj = Integer.parseInt(data.gmt_end) - Integer.parseInt(data.gmt_begin);
                    message.what = UPTIME;
                    handler.sendMessage(message);
                    recyclerviewheader.setTitle(data.title);
                    sendBitmap(data.main_img, BIGIMAGE);
                    sendBitmap(data.mj_icon, ICON);
                }
            }


        });
    }

    public  void sendBitmap(final String path, final int what){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Bitmap bitmap= Picasso.with(ItemActivity.this).load(path).get();
                    Message message=Message.obtain();
                    message.obj=bitmap;
                    message.what=what;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



    @Override
    protected void getIntentData(Bundle saveInstance) {


    }

    @Override
    public void onClick(View v) {


    }

    @Override
    public void onBackPressed() {
       finish();
    }
}
